//
//  AIInfoThirdTableCell.m
//  Mia
//
//  Created by Vill on 2023/12/20.
//

#import "AIInfoThirdTableCell.h"

@interface AIInfoThirdTableCell ()

@property (nonatomic, strong) UIButton *selectBtn;  // 选中的
@property (nonatomic, assign) BOOL ischoose;
@property (nonatomic, copy) NSArray *buttonArray;

@end

@implementation AIInfoThirdTableCell

-(NSArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [NSArray arrayWithObjects:@"Female",@"Male",@"Secrecy",nil];
    }
    return _buttonArray;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"AIInfoThirdTableCell";
    AIInfoThirdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    }
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self initSubviews];
}

- (void)initSubviews {
    CGFloat btnWidth = (KScreenWidth - PTW(75))/3;
    CGFloat bthHeight =  PTW(48);
    for (int i = 0; i < self.buttonArray.count ; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(PTW(27.5) + (btnWidth + PTW(10))*i , PTW(12.5), btnWidth, bthHeight)];
        btn.tag = i + 70;
        [self.contentView addSubview:btn];
        [btn addTarget:self action:@selector(reasonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor colorHex:@"#FFFFFF" alpha:0.3];
        [btn setTitle:self.buttonArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorHex:@"#FFFFFF"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        btn.layer.cornerRadius = PTW(24);
        btn.layer.borderWidth = PTW(1);
        btn.layer.borderColor = UIColor.clearColor.CGColor;
        btn.layer.shadowColor = [UIColor clearColor].CGColor;
        btn.layer.shadowOffset = CGSizeMake(0,3);
        btn.layer.shadowOpacity = 1;
        btn.layer.shadowRadius = 2.5;
        if(btn.tag == 70){
            btn.selected = YES;
            btn.layer.borderColor = UIColor.whiteColor.CGColor;
            btn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
        }
    }
}

// 选中的性别
- (void)setChooseStr:(NSString *)chooseStr{
    _chooseStr = chooseStr;
    NSLog(@"%@",chooseStr);
    if ([chooseStr isEqualToString:@"Female"]){
        UIButton *btn = (UIButton *)[self viewWithTag:70];
        btn.layer.borderColor = UIColor.whiteColor.CGColor;
        btn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
        btn.selected = YES;
    }else if ([chooseStr isEqualToString:@"Male"]){
        UIButton *btn = (UIButton *)[self viewWithTag:71];
        btn.layer.borderColor = UIColor.whiteColor.CGColor;
        btn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
        btn.selected = YES;
        
        UIButton *but = (UIButton *)[self viewWithTag:70];
        but.layer.borderColor = UIColor.clearColor.CGColor;
        but.layer.shadowColor = [UIColor clearColor].CGColor;
        but.selected = NO;
    }else{
        UIButton *btn = (UIButton *)[self viewWithTag:72];
        btn.layer.borderColor = UIColor.whiteColor.CGColor;
        btn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
        btn.selected = YES;
        
        UIButton *but = (UIButton *)[self viewWithTag:70];
        but.layer.borderColor = UIColor.clearColor.CGColor;
        but.layer.shadowColor = [UIColor clearColor].CGColor;
        but.selected = NO;
    }
}

// 按钮点击
-(void)reasonClick:(UIButton *)btn{
    self.ischoose = YES;
    if (btn == self.selectBtn) {
        return;
    }
    for (int i = 0; i < self.buttonArray.count; ++i) {
        if (btn.tag == 70 + i) {
            btn.selected = YES;
            self.selectBtn = btn;
            btn.layer.borderColor = UIColor.whiteColor.CGColor;
            btn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
            continue;
        }
        UIButton *but = (UIButton *)[self viewWithTag:70+i];
        but.layer.borderColor = UIColor.clearColor.CGColor;
        but.layer.shadowColor = [UIColor clearColor].CGColor;
        but.selected = NO;
    }
    if (self.sexSelectBlock){
        self.sexSelectBlock(self.buttonArray[btn.tag-70]);
    }
}


@end
